<?php

namespace App\Controller\AdminApi;

use App\Base\BaseController4Back;
use App\Entity\AdminGroup;
use App\Entity\AdminMenu;
use App\Repository\AdminMenuRepository;
use App\Repository\AdminUserRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;

/**
 * @Route("/admin/api")
 */
class FrameController extends BaseController4Back
{

    /**
     * @Route("/login", name="AdminApiLogin")
     */
    public function BackLoginDo(AdminUserRepository $repository,Request $request)
    {
        $data = $this->getRequestData();
        $name =$data["username"]??"";
        $password =$data["password"]??"";
        $admin = $repository->findOneBy(array("name"=>$name,"password"=>"$password","enable"=>true));
        if (!$admin) {
            //账号或者密码错误
            return $this->json_out(0,"账号或者密码错误");
        }else{
            $roles = $admin->getGroups()->getValues();
            $sessRole = [];
            foreach ($roles as $role) {
                $sessRole[] = $role->getRole();
            }
            $this->setUserCache("role",$sessRole);
            $this->setUserCache("id",$admin->getId());
            return $this->json_out(1,"登陆成功");
        }
    }


    /**
     * @Route("/frame", name="BackIndexFrame")
     */
    public function BackIndexFrame(AdminUserRepository $repository,SerializerInterface $serializer,AdminMenuRepository $adminMenuRepository)
    {
        $uid = $this->needToBeAdmin();
        $admin = $repository->find($uid);
        $roles = $admin->getGroups()->getValues();
        $got = [];
        foreach ($roles as $role) {
            /**@var $role AdminGroup*/
            $menus = $role->getMenus()->getValues();
            foreach ($menus as $menu) {
                $got[] = $menu->getId();
            }
        }
        $data=[];
        //按钮功能应该在用户登录时确定
        $menuTree = $adminMenuRepository->getAllAdminMenu();
        $temp = [];
        $this->serializer = $serializer;
        $menuTree = $this->getSons($menuTree,$got);
        $data["menuTree"] = $menuTree;
        $data["admin"] = $admin;
        return $this->json_out(1,"OK",$data);
    }

    public function getSons($array,$ids)
    {
        $tem = [];
        foreach ( $array as $item){
            /**@var $item AdminMenu*/
            if (in_array($item->getId(),$ids)) {
                $sons = $item->getSons()->getValues();
                $arr = $this->serializer->normalize($item,"array");

                $arr["sons"] = $this->getSons($sons,$ids);
                $tem[] = $arr;
            }
        }
        return $tem;
    }


    /**
     * @Route("/welcome", name="BackFrameWelcome")
     */
    public function BackFrameWelcome(AdminUserRepository $repository)
    {
        $data=[];
        return $this->show("welcome",$data);
    }

    /**
     * @Route("/logout", name="BackFrameLogout")
     */
    public function BackFrameLogout(AdminUserRepository $repository)
    {
        $this->mySession()->clear();
        return $this->redirectToRoute("BackLogin");
    }




}
